package com.lemonboxstudio.drawboard.ui

import android.os.Bundle
import com.cjz.jnidrawfb.DrawFrameBuffer
import com.lemonboxstudio.drawboard.data.Mode
import com.lemonboxstudio.drawboard.databinding.ActivityMainBinding
import com.lemonboxstudio.drawboard.widget.DrawCallBack

class MainActivity : BaseActivity() {
    private lateinit var binding : ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setListener()
        changePanStatus()
    }

    private fun setListener() {
        binding.tvUndo.setOnClickListener {
            binding.drawView.undo()
        }

        binding.tvRedo.setOnClickListener {
            binding.drawView.redo()
        }

        binding.tvEraser.setOnClickListener {
            binding.drawView.setMode(Mode.ERASER)
            changePanStatus()
        }

        binding.tvPan.setOnClickListener {
            binding.drawView.setMode(Mode.DRAW)
            changePanStatus()
        }

        binding.tvClear.setOnClickListener {
            binding.drawView.clear()
        }

        binding.drawView.setOnDrawCallBack(object : DrawCallBack{
            override fun onUndoRedoStatusChanged() {
                binding.tvUndo.isEnabled = binding.drawView.canUndo
                binding.tvRedo.isEnabled = binding.drawView.canRedo
                changePanStatus()
            }
        })
    }

    private fun changePanStatus() {
        binding.tvPan.isEnabled = binding.drawView.getMode() != Mode.DRAW
        binding.tvEraser.isEnabled = binding.drawView.getMode() != Mode.ERASER
    }

    override fun onResume() {
        super.onResume()
        //DrawFrameBuffer.initFb()
    }

    override fun onPause() {
        //DrawFrameBuffer.closeFb()
        super.onPause()
    }
}